#include <cstddef>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

#include "include\battle.h"
#include "include\entitymanager.h"
#include "include\entity.h"
#include "include\action.h"


using namespace std;


BattleLoop::BattleLoop()
{
    srand(time(NULL)); // Seed random number generator
    EntityManager *pEntityManager = EntityManager::GetInstance();
    pEnemy = pEntityManager->CreateNewEntity("Baddie");
    pEnemy->SetMaxHealth(300);
    pEnemy->SetHealth(300);
    pPlayer = pEntityManager->pPlayer;
    pPlayer->SetMaxHealth(100);
    pPlayer->SetHealth(100);
}

void BattleLoop::StartBattle() // Main loop for battle
{
    playerDeath = false;
    while ((pEnemy->GetHealth() != 0) && (pPlayer->GetHealth() != 0))
    {
        OneTurn();
    }

    if (!playerDeath)
    {
        cout << "Yay, a battle happened, and you won. You are special." << endl;
    }
    else
    {
        cout << "You died. How sad." << endl;
    }
}

void BattleLoop::OneTurn()
{
    system("cls");
    pPlayer->Display();
    pEnemy->Display();

    pPlayer->DisplayLastAction();
    pEnemy->DisplayLastAction();

    ActionType thisAction = NOTHING;
    while (thisAction == NOTHING)
    {
        thisAction = pPlayer->getAction();
    }
    pPlayer->ExecuteAction(thisAction, pPlayer, pEnemy);

    pEnemy->ExecuteAction(pEnemy->getAction(), pEnemy, pPlayer);

    pPlayer->NewTurn();
    pEnemy->NewTurn();



    if (pPlayer->GetHealth() == 0)
    {
        playerDeath = true;
        return;
    }
}


